package com.ljb.getui_demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.igexin.sdk.PushManager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    //定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
    private static String appId = "49mL4PHVbc9nS6ch5JGXu2";
    private static String appKey = "HbtWtTrhhD7aqDEuem7vj";
    private static String masterSecret = "At5bxPx4bi8yztCfYPU1fA";
    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
    private EditText et_title;
    private EditText et_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PushManager.getInstance().initialize(this.getApplicationContext(), PushService.class);
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), IntentService.class);

        et_title = (EditText) findViewById(R.id.et_title);
        et_content = (EditText) findViewById(R.id.et_content);
    }

    public void push(View view) {

        push();
    }

    private void push() {
        new Thread() {
            @Override
            public void run() {
                IGtPush push = new IGtPush(url, appKey, masterSecret);

                // 定义"点击链接打开通知模板"，并设置标题、内容、链接
                NotificationTemplate template = new NotificationTemplate();
                template.setAppId(appId);
                template.setAppkey(appKey);
                template.setTitle(et_title.getText().toString().trim());
                template.setText(et_content.getText().toString().trim());

                List<String> appIds = new ArrayList<>();
                appIds.add(appId);

                // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
                AppMessage message = new AppMessage();
                message.setData(template);
                message.setAppIdList(appIds);
                message.setOffline(true);
                message.setOfflineExpireTime(1000 * 600);

                IPushResult ret = push.pushMessageToApp(message);
                String result = (String) ret.getResponse().get("result");
                Log.e("hehe", ret.getResponse().toString());

                if (TextUtils.equals("ok", result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "推送成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
//                System.out.println(ret.getResponse().toString());
            }
        }.start();
    }
}
